<template>
  <div>
    <el-button
      class="border-btn"
      size="small"
      style="margin-bottom: 20px;"
      @click="handlAdd"
    >新建合同</el-button>
    <el-table
      :data="tableData"
      header-cell-class-name="table-header"
      size="medium"
      border
      :cell-style="cellStyle"
      @row-click="aloneClick"
    >
      <el-table-column
        type="contractNumber"
        label="合同编号"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="contractName"
        label="合同名称"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="contractBeginTime"
        label="开始日期"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template #default="{row}">
          {{ dateFormat("yyyy-mm-dd HH:MM:SS",row.contractBeginTime) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="contractEndTime"
        label="结束日期"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template #default="{row}">
          {{ dateFormat("yyyy-mm-dd HH:MM:SS",row.contractEndTime) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="contractHeadName"
        label="合同负责人"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="createdAt"
        label="创建时间"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template #default="{row}">
          {{ dateFormat("yyyy-mm-dd HH:MM:SS",row.createdAt) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="状态"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template #default="{row}">
          {{ getLabel('contract_status', row.contractStatus) }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
      >
        <template #default="{row}">
          <template v-if="row.contractStatus == -1">
            <el-button
              type="text"
              @click="handlEdit(row)"
            >编辑</el-button>
            <el-button
              type="text"
              @click="handlDele(row.id)"
            >删除</el-button>
          </template>
          <el-button
            type="text"
            @click="handlEnd(row.id)"
          >履约结束</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--新建合同-->
    <!--    <addContract ref="addContractDialog" @succsess="succsess"/>-->
    <!--    &lt;!&ndash;合同详情&ndash;&gt;-->
    <!--    <detail-drawer ref="detailDrawer" custom-title :sizeWidth="1000">-->
    <!--      <template #title>-->
    <!--        <span>兼职合同详情 </span>-->
    <!--      </template>-->
    <!--      <template #contents v-if="contractId !== 0">-->
    <!--        <div class="drawer-content">-->
    <!--          <div class="history-list">-->
    <!--            <sales-detail ref="salesDetailDrawer" :contractId="contractId"/>-->
    <!--          </div>-->
    <!--        </div>-->
    <!--      </template>-->
    <!--    </detail-drawer>-->
  </div>
</template>

<script>
import {getCurrentInstance, inject, reactive, toRefs,} from 'vue'
import {useRoute, useRouter} from 'vue-router'

export default {
  name: 'PartTimeContract',
  components: {
    // addContract: defineAsyncComponent(() => import('../../../terminal/modules/addContract.vue')),
    // salesDetail: defineAsyncComponent(() => import('../../../terminal/modules/salesDetail.vue'))
  },
  props: {
    formData: {
      type: Object,
      defaults() {
        return {
          agent: {},
          contract: {},
        }
      },
    },
  },
  setup(props, context) {
    const route = useRoute()
    const router = useRouter()
    const currentInstance = getCurrentInstance()
    const api = inject('api')
    var data = reactive({
      tableData: [],
      addContractDialog: null,
      salesDetailDrawer: null,
      detailDrawer: null,
      typeDate: {
        id: props.formData.agent.id,
        name: props.formData.agent.userName,
        contractType: 'agent',
        contractObjectType: 'part_time',
      },
      queryParams: {
        relationId: Number(route.query.id),
        contractObjectType: 'part_time',
        contractType: 'agent',
        pageSize: 20,
        page: 1,
      },
      contractId: 0,
    })

    const handlAdd = () => {
      data.addContractDialog.open(data.typeDate, null)
    }

    // 获取列表
    const handlList = () => {
      api.terminal.SALES_LIST(data.queryParams).then((res) => {
        if (res.code === 0) {
          data.tableData = res.data.list || []
          data.total = res.data.total
        }
      })
    }

    // 编辑
    const handlEdit = (row) => {
      data.addContractDialog.open(data.typeDate, row)
    }

    // 删除
    const handlDele = (id) => {
      currentInstance.proxy
        .alertBox({
          message: '是否确认删除该兼职合同',
          title: '兼职合同删除',
        })
        .then(() => {
          api.terminal.SALES_DELETE(id).then((res) => {
            if (res.code) {
              currentInstance.proxy.customMessage({
                type: 'error',
                message: res.msg,
              })
            } else {
              handlList()
            }
          })
        })
        .catch((err) => {
          console.log(err)
        })
    }

    // 终止
    const handlEnd = (id) => {
      currentInstance.proxy
        .alertBox({
          message: '是否确认履约结束该合同',
          title: '履约结束',
        })
        .then(() => {
          api.terminal.SALES_END(id).then((res) => {
            if (res.code) {
              currentInstance.proxy.customMessage({
                type: 'error',
                message: res.msg,
              })
            } else {
              handlList()
            }
          })
        })
        .catch((err) => {
          console.log(err)
        })
    }

    // 刷新列表
    const succsess = () => {
      handlList()
    }

    const aloneClick = (row, column) => {
      if (column.label === '操作') return
      data.contractId = row.id
      data.detailDrawer.open()
    }

    const cellStyle = () => {
      return 'cursor:pointer;'
    }

    return {
      ...toRefs(data),
      handlAdd,
      handlList,
      handlEdit,
      aloneClick,
      handlDele,
      succsess,
      cellStyle,
      handlEnd,
    }
  },
}
</script>

<style scoped>
/* @import url(); 引入css类 */
</style>
